package com.xumz.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xumz.controller.tool.IDFactoty;
import com.xumz.dao.ClassDao;
import com.xumz.dao.ClassSettingDao;
import com.xumz.dao.TeacherDao;
import com.xumz.model.ClassName;
import com.xumz.model.ClassSetting;
import com.xumz.model.StudentManage;
import com.xumz.model.Teacher;
import com.xumz.service.ClassService;
@Service
public class ClassServiceImpl implements ClassService {
	@Autowired
	private ClassDao cmDao;
	@Autowired
	private ClassSettingDao classSettingDao;
	@Autowired
	private TeacherDao teacherDao;
	
	@Override
	public List<StudentManage> selectAll() {
		List<StudentManage> list = cmDao.selectAll();
		return list;
	}
	
	@Override
	public ClassName findClassNameById(String id) {
		return cmDao.selectOne(id);
	}
	
	@Override
	public List<StudentManage> selectAllStudentByTeacherName(String name) {
		Teacher teacher=teacherDao.selectOne(name);
		List<StudentManage> list = cmDao.selectAllByTeacherId(teacher.getId());
		return list;
	}
	
	@Override
	public List<ClassName> selectClassName() {
		List<ClassName> list = cmDao.selectClassName();
		return list;
	}
	
	@Override
	public List<ClassName> selectClassNameByTeacherName(String id) {
		Teacher teacher=teacherDao.selectOne(id);
		List<ClassName> list = cmDao.selectClassNameByTeacherId(teacher.getId());
		return list;
	}

	
	@Override
	public boolean classgroupAdd(String className,String classdescribe,String tname){
		Teacher teacher=teacherDao.selectOne(tname);
		String teacherId=teacher.getId();
		String classId=IDFactoty.productIdByDateTime();
		ClassSetting classSet=new ClassSetting(IDFactoty.productIdByDateTime(),
				teacherId, classId);
		boolean flag2=cmDao.classgroupAdd(classId,className, classdescribe) > 0 ? true : false;
		boolean flag1=classSettingDao.insertOne(classSet) > 0 ? true : false;
		return flag1&&flag2;
	}
	
	@Override
	public boolean classgroupUpdate(ClassName classname){
		return cmDao.classgroupUpdate(classname) > 0 ? true : false;
	}
	
	@Override
	public boolean classgroupDelete(String classid){
		return cmDao.classgroupDelete(classid) > 0 ? true : false;
	}

	@Override
	public String classNameById(String id) {
		List<ClassName> classList= cmDao.selectClassName();
		for (int i = 0; i < classList.size(); i++) {
			if (classList.get(i).getClassId().equals(id)) {
				return classList.get(i).getClassName();
			}
		}
		return null;
	}
	
}
